package com.lianboke.link.ad.updateUtils;

import android.text.TextUtils;
import android.util.Log;

import com.lianboke.link.ad.utils.KLog;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 陈丰尧 on 2017/4/16.
 */

public class SystemManager {

    /**
     * 应用程序运行命令获取 Root权限，设备必须已破解(获得ROOT权限)
     *
     * @param command 命令：String apkRoot="chmod 777 "+getPackageCodePath();
     * @return  0 命令执行成功
     */
    public static int RootCommand(String command) {
        KLog.e("log_root","---------RootCommand-----command------:"+command);
        Process process = null;
        DataOutputStream os = null;
        BufferedReader successResult = null;
        BufferedReader errorResult = null;
        StringBuilder successMsg = null;
        StringBuilder errorMsg = null;
        try {
            //静默安装需要root权限
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.write(command.getBytes());
            os.writeBytes("\n");
            os.writeBytes("exit\n");
            os.flush();
            //执行命令
            process.waitFor();
            //获取返回结果
            successMsg = new StringBuilder();
            errorMsg = new StringBuilder();
            successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
            errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String s;
            while ((s =successResult.readLine()) != null) {
                successMsg.append(s);
            }
            while ((s = errorResult.readLine())!= null) {
                errorMsg.append(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
            KLog.e("log_root","---------RootCommand-----Exception------:"+e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
                if (successResult != null) {
                    successResult.close();
                }
                if (errorResult != null) {
                    errorResult.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        KLog.e("log_root","---------RootCommand-----errorMsg------:"+errorMsg);
        KLog.e("log_root","---------RootCommand-----successMsg------:"+successMsg);
        if(TextUtils.isEmpty(errorMsg.toString().trim()) && TextUtils.isEmpty(successMsg.toString().trim())){
            return 0;
        }else if(TextUtils.isEmpty(errorMsg.toString().trim()) && !TextUtils.isEmpty(successMsg.toString().trim())){
            return 0;
        }else if(!TextUtils.isEmpty(errorMsg.toString().trim()) ){
            return -1;
        }else {
            return -1;
        }


//        Process process = null;
//        DataOutputStream os = null;
//        try {
//            process = Runtime.getRuntime().exec("su");
//            os = new DataOutputStream(process.getOutputStream());
//            os.writeBytes(command + "\n");
//            os.writeBytes("exit\n");
//            os.flush();
//            int i = process.waitFor();
//
//            Log.d("SystemManager", "i:" + i);
//            return i;
//        } catch (Exception e) {
//            Log.d("SystemManager", e.getMessage());
//            return -1;
//        } finally {
//            try {
//                if (os != null) {
//                    os.close();
//                }
//                process.destroy();
//            } catch (Exception e) {
//            }
//        }
    }

    /**
     * 提升读写权限
     * @param filePath 文件路径
     * @return
     * @throws IOException
     */
    public static void setPermission(String filePath)  {
        String command = "chmod " + "777" + " " + filePath;
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec(command);
        } catch (Exception e) {
            e.printStackTrace();
            KLog.e("log_root","---------setPermission-----exception------:"+e.getMessage());
        }
    }

}
